//
//  RAChapterListController.m
//  ReadApp
//
//  Created by 飞猪 on 2018/11/4.
//  Copyright © 2018年 Facebook. All rights reserved.
//

#import "RAChapterListController.h"
#import "RAChapterListModel.h"
#import "RABookMallController.h"

@interface RAChapterListController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *models;

@end

@implementation RAChapterListController

- (void)viewDidLoad {
    [super viewDidLoad];
  
  self.view.backgroundColor = [UIColor whiteColor];
  
  self.navigationItem.title = @"章节列表";
  
  [self.view addSubview:self.tableView];

  [self requestForChapterList];
}

- (void)viewWillLayoutSubviews {
  [super viewWillLayoutSubviews];
  
  self.tableView.frame = self.view.bounds;
}

- (void)requestForChapterList {
  
  NSDictionary *params = @{
                           @"bookId": _model.bookId ?: @""
                           };
  [SVProgressHUD showWithStatus:@"加载中..."];
  [SVProgressHUD setFont:[UIFont boldSystemFontOfSize:14.f]];
  [SVProgressHUD setForegroundColor:[UIColor grayColor]];
  [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
  [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
  [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeNative];
  
  [RDNetworkTool requestForChapterList:params successBlock:^(NSArray<RDNETChapterListModel *> *models) {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
      [SVProgressHUD dismiss];
      
      [self.models addObjectsFromArray:models];
      
      [self.tableView reloadData];
      
    });
  } failBlock:^(NSString *errorMsg) {
    [SVProgressHUD dismiss];
    [SVProgressHUD showErrorWithStatus:errorMsg];
  }];
  
}

#pragma mark - Event
- (void)beginRead:(RDNETChapterListModel *)model {
  
  RDReader *reader = [[RDReader alloc] init];
  
  RDConfiguration *config = [[RDConfiguration alloc] init];
  NSString *imgstr = [[NSUserDefaults standardUserDefaults] objectForKey:@"readerBGImage"];
  if (imgstr && ![imgstr isEqualToString:@"A39470"]) {
    UIColor *col = [UIColor colorWithHexString:imgstr];
    if (col) {
      config.readerBGImage = [UIImage imageWithColor:col];
    }
    else {
      config.readerBGImage = [UIImage imageNamed:@"yellow"];
    }
  }
  else {
    config.readerBGImage = [UIImage imageNamed:@"yellow"];
  }
  
  reader.config = config;
  
  RDReaderDelegateController *readerDelegateController = [RDReaderDelegateController new];
  [self addChildViewController:readerDelegateController];
  readerDelegateController.reader = reader;
  reader.delegate = readerDelegateController;
  
  [self presentViewController:reader animated:YES completion:nil];
  
  NSDictionary *params = @{
                           @"bookId" : model.bookId ?: @"",
                           @"chId" : model.chId ?: @"0"
                           };
  [reader beginWithParam:params];
  
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
  
  RDNETChapterListModel *model = self.models[indexPath.row];
  cell.textLabel.text = model.chName;
  cell.textLabel.font=[UIFont systemFontOfSize:12.f];
  cell.textLabel.textColor = [UIColor blackColor];
  
  return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView deselectRowAtIndexPath:indexPath animated:YES];
  
  RDNETChapterListModel *model = self.models[indexPath.row];
  [self beginRead:model];
  
}

#pragma mark - Lazyload

- (UITableView *)tableView {
  if (!_tableView) {
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    _tableView.tableFooterView = [UIView new];
  }
  return _tableView;
}

- (NSMutableArray *)models {
  if (!_models) {
    _models = [NSMutableArray array];
  }
  return _models;
}

@end
